package core

import (
	"time"
)

// Config 嘉联支付API配置
type Config struct {
	// AppID 应用ID
	AppID string
	// PrivateKey SM2私钥
	PrivateKey string
	// JlpayPublicKey 嘉联支付公钥
	JlpayPublicKey string
	// BaseURL API基础URL，例如：https://openapi.jlpay.com
	BaseURL string
	// AutoEncrypt 是否自动加密敏感数据
	AutoEncrypt bool
	// AutoDecrypt 是否自动解密敏感数据
	AutoDecrypt bool
	// CryptoAlg 加密算法，例如：SM2WithSM4
	CryptoAlg string
	// ConnectTimeout 连接超时时间
	ConnectTimeout time.Duration
	// ReadTimeout 读取超时时间
	ReadTimeout time.Duration
}

// NewConfig 创建新的配置
func NewConfig() *Config {
	return &Config{
		BaseURL:        "https://openapi.jlpay.com",
		AutoEncrypt:    true,
		AutoDecrypt:    true,
		CryptoAlg:      "SM2WithSM4",
		ConnectTimeout: 10 * time.Second,
		ReadTimeout:    30 * time.Second,
	}
}

// WithAppID 设置应用ID
func (c *Config) WithAppID(appID string) *Config {
	c.AppID = appID
	return c
}

// WithPrivateKey 设置SM2私钥
func (c *Config) WithPrivateKey(privateKey string) *Config {
	c.PrivateKey = privateKey
	return c
}

// WithJlpayPublicKey 设置嘉联支付公钥
func (c *Config) WithJlpayPublicKey(publicKey string) *Config {
	c.JlpayPublicKey = publicKey
	return c
}

// WithBaseURL 设置API基础URL
func (c *Config) WithBaseURL(baseURL string) *Config {
	c.BaseURL = baseURL
	return c
}

// WithAutoEncrypt 设置是否自动加密
func (c *Config) WithAutoEncrypt(autoEncrypt bool) *Config {
	c.AutoEncrypt = autoEncrypt
	return c
}

// WithAutoDecrypt 设置是否自动解密
func (c *Config) WithAutoDecrypt(autoDecrypt bool) *Config {
	c.AutoDecrypt = autoDecrypt
	return c
}

// WithCryptoAlg 设置加密算法
func (c *Config) WithCryptoAlg(cryptoAlg string) *Config {
	c.CryptoAlg = cryptoAlg
	return c
}

// WithConnectTimeout 设置连接超时时间
func (c *Config) WithConnectTimeout(timeout time.Duration) *Config {
	c.ConnectTimeout = timeout
	return c
}

// WithReadTimeout 设置读取超时时间
func (c *Config) WithReadTimeout(timeout time.Duration) *Config {
	c.ReadTimeout = timeout
	return c
}

// Build 完成构建并返回配置对象
func (c *Config) Build() *Config {
	return c
}
